package com.lkm.app_ui.m.Imple.fun;

import android.os.Handler;
import android.os.Looper;

import com.lkm.app_ui.AppConst;
import com.lkm.app_ui.event.RequestInstallApkEvent;
import com.lkm.app_ui.help.AppHelp;
import com.lkm.app_ui.m.fun.FunModule;
import com.lkm.comlib.Config;
import com.lkm.frame.otto.LinkBus;
import com.lkm.frame.otto.ObserverAutoNext;
import com.lkm.frame.task2.Task;
import com.lkm.javatool.CollectionHelp;
import com.lkm.javatool.StringHelp;
import com.lkm.javatool.VersionNumHelp;
import com.lkm.market2_0.task.AsyncTask2;
import com.lkm.market2_0.task.DefaultTaskExecutor;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;




public class ApkInstall {

    private FunModule funModule;

    public ApkInstall(FunModule funModule) {
        onRequestInstallApkEvent();
        this.funModule = funModule;


    }



    public void installPreDownload(){
        Task task = new CheckInstallTask();
        task.action();
        task.execTask();
    }

    private class CheckInstallTask extends AsyncTask2 {

        public CheckInstallTask() {
            super(new DefaultTaskExecutor(true), CheckInstallTask.class.getName(), AppConst.getTaskManager2());
        }
        @Override
        protected void doAsyncExecuting(Object param) {

            String curVersion = AppHelp.getLocalVersionName();
            File apkDir = new File(Config.getApkFileCache(AppConst.getApplication()));
            
            String apkFiles [] = apkDir.list();
            List<String> newVersions = new ArrayList<>();

            String newApkPath = "";
            if(!CollectionHelp.isEmpty(apkFiles)){
                for (int i = 0; i < apkFiles.length; i++) {
                    
                    if(!apkFiles[i].endsWith("apk")){
                        continue;
                    }

                    String version = apkFiles[i].replaceAll(".apk","");
                    if(VersionNumHelp.compareVersion(version,curVersion)==1){
                        
                        newVersions.add(version);
                    }
                }
//                newVersions.add("1.0.20011501");
//                newVersions.add("1.0.20011201");

                if(!CollectionHelp.isEmpty(newVersions)) {
                    Collections.sort(newVersions, new Comparator<String>() {
                        @Override
                        public int compare(String o1, String o2) {
                            return VersionNumHelp.compareVersion(o2, o1);
                        }
                    });
                    newApkPath = Config.getApkFileCache(AppConst.getApplication()) + newVersions.get(0) + ".apk";
                }else {
                    newApkPath = "";
                }
            }

            final String newApkPath_ = newApkPath;
            
            if(!StringHelp.isEmpty(newApkPath)){
                mTaskExecutor.executeOnDispatch(new Runnable() {
                    @Override
                    public void run() {
                        AppHelp.installApk(newApkPath_);
                    }
                });
            }


        }
    }

    public String apkPath = null;




    private void onRequestInstallApkEvent() {
        LinkBus.registerReceiver(new ObserverAutoNext<RequestInstallApkEvent>() {
            @Override
            public Class<RequestInstallApkEvent> getBusEventClass() {
                return RequestInstallApkEvent.class;
            }

            @Override
            public void onReceiver(RequestInstallApkEvent data) {
                apkPath = data.apkPath;
                
                if (!funModule.isUserHandling()) {
                    AppHelp.installApk(apkPath);
                    apkPath = null;
                }
            }
        });

    }
}
